package org.lq.ssm.service.impl;

import org.lq.ssm.dto.PermissionDTO;
import org.lq.ssm.dto.RoleDTO;
import org.lq.ssm.dto.UserDTO;
import org.lq.ssm.dto.mapper.RoleDTOMapper;
import org.lq.ssm.dto.mapper.UserDTOMapper;
import org.lq.ssm.mapper.*;
import org.lq.ssm.po.*;
import org.lq.ssm.service.RoleService;
import org.lq.ssm.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

@Service
public class UserServiceImpl implements UserService {

    //单一接口实现类时@Resource(jdk的)，@Autowired(spring的)的效果一样
    @Resource
    UserPOMapper userPOMapper;
    @Resource
    JobPOMapper jobPOMapper;
    @Resource
    DeptPOMapper deptPOMapper;
    @Resource
    RolePOMapper rolePOMapper;
    @Resource
    UsersRolesMapper usersRolesMapper;
    @Resource
    RoleService roleService;

    @Override
    public UserDTO login(String username, String password) {
        Long userId = userPOMapper.selectByNamePass(username, password);
        if (userId==-1){
            return null;
        }
        else {
            return getAllUser(-1, userId).get(0);
        }
    }



    @Override
    public List<UserDTO> getAllUser(int level,long id) {
        UserPO userPO = userPOMapper.selectByPrimaryKey(id);
        List<UserPO> userPOS = new LinkedList<>();
        if (level!=-1) {
            userPOS = userPOMapper.selectDownLevelUsers(level);
        }
        System.out.println(userPO);
        userPOS.add(userPO);
        List<UserDTO> userDTOS = new LinkedList<>();
        for (int i = 0;i<userPOS.size()&&userPOS!=null;i++) {
            UserPO everyUserPO = userPOS.get(i);
            JobPO jobPO = jobPOMapper.selectByPrimaryKey(userPOS.get(i).getJobId());
            DeptPO deptPO = deptPOMapper.selectByPrimaryKey(userPOS.get(i).getDeptId());
            UserDTO userDTO = UserDTOMapper.INSTANCE.userToDTO(everyUserPO, deptPO, jobPO);
            List<RolePO> rolePOS = getRoles(userDTO);
            userDTO.setRole(RoleDTOMapper.INSTANCE.RolePOSTORoleDTOS(rolePOS));
            userDTOS.add(userDTO);
        }
        return userDTOS;
    }

    @Override
    public void addUser(UserDTO userDTO) {

    }

    @Override
    public List<String> allRolesToPermissionName(UserDTO userDTO){
        Set<String> permissionDTOSets = new HashSet<>();

        for(RoleDTO roleDTO :userDTO.getRole()){
            List<PermissionPO> permissions = roleService.getPermissions(RoleDTOMapper.INSTANCE.RoleDTOToRolePO(roleDTO));
            for (PermissionPO permissionPO:permissions){
                permissionDTOSets.add(permissionPO.getName());
            }
        }
        List<String> permissionsName = new LinkedList<>();
        for (String permissionName : permissionDTOSets){
            permissionsName.add(permissionName);
        }
        return permissionsName;
    }




    private List<RolePO> getRoles(UserDTO userDTO){
        UsersRolesExample usersRolesExample = new UsersRolesExample();
        UsersRolesExample.Criteria criteria = usersRolesExample.createCriteria();
        criteria.andUserIdEqualTo(userDTO.getId());
        List<UsersRolesKey> usersRolesKeys = usersRolesMapper.selectByExample(usersRolesExample);
        List<RolePO> rolePOS = new LinkedList<>();
        for (UsersRolesKey usersRolesKey : usersRolesKeys){
            RolePO rolePO = rolePOMapper.selectByPrimaryKey(usersRolesKey.getRoleId());
            rolePOS.add(rolePO);
        }
        return rolePOS;
    }
}
